import { UserInfo, UserProfile } from '@/types/data'
import { UserInfoAction } from '@/types/store'

type ProfileState = {
  userinfo: UserInfo
  profile: UserProfile
}

// as 类型转换
// 如果token有误,直接提示去登陆
// 如果可以获取到用户信息 字段和定义的类型是对上的
function profileReducer(state: ProfileState = { userinfo: {}, profile: {} } as ProfileState, action: UserInfoAction) {
  switch (action.type) {
    case 'profile/userinfo':
      return { ...state, userinfo: action.payload }
    case 'profile/profile':
      return { ...state, profile: action.payload }
    default:
      return state
  }
}

export default profileReducer
